<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/

	$GLOBALS['OUTPUT']->master_section = 'management';
	$GLOBALS['OUTPUT']->minor_section = $GLOBALS['LANG']->lang_admin->manageusers;
	$GLOBALS['OUTPUT']->cpmenu = $menus[$GLOBALS['OUTPUT']->master_section];
	
	$query = x7in::get($_REQUEST,'query');
	$users = array();
	
	$del = x7in::read('del','i');
	if(!empty($del)){
		$test = new lib_user($del);
		if($test->is_valid()){
			$test->remove();
			$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->userdeleted;
		}
	}
	
	// Obtain a list of groups
	$groups = array();
	$riterator = $GLOBALS['DATABASE']->get('groups');
	while($gdata = $riterator->get_next()){
		$groups[$gdata['gid']] = $gdata['name'];
	}
	
	$form = new form();
	
	$form->add_field('text','username',$GLOBALS['LANG']->lang_admin->username);
	$form->add_field('text','password',$GLOBALS['LANG']->lang_admin->password);
	$form->add_field('text','email',$GLOBALS['LANG']->lang_admin->email);
	$form->add_field('select','group',$GLOBALS['LANG']->lang_admin->group,'',array_flip($groups));
	
	$username = $form->get_add_default('username');
	if(!empty($username)){
		$pword1 = $form->get_add_default('password');
		$email = $form->get_add_default('email');
		$group = $form->get_add_default('group');
		
		if(empty($pword1)){
			$form->add_error('password',$GLOBALS['LANG']->lang_admin->invalid_password);
			$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->unabletocreateuser;
		}else{
			$account_create_attempt = lib_user_extended::create_user_account($username,$pword1,$group,array('email'=>$email),true);
			
			if(is_array($account_create_attempt)){
				
				foreach($account_create_attempt as $error){
					switch($error){
						case 'username_invalid':
						case 'username_in_use':
							$form->add_error('username',$GLOBALS['LANG']->lang_admin->$error);
						break;
						case 'email_in_use':
							$form->add_error('email',$GLOBALS['LANG']->lang_admin->$error);
						break;
						case 'invalid_group':
							$form->add_error('group',$GLOBALS['LANG']->lang_admin->$error);
						break;
						default:
							$form->add_error('username',str_replace('_c',$error,$GLOBALS['LANG']->lang_admin->fatal_reg));
						break;
					}
				}
				
				$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->unabletocreateuser;
			}else{
				$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->usercreated;
			}
		}
		
	}
	
	if(empty($query)){
		
		// Obtain a list of users
		// FIXME: THis code is broken
		$pager = new lib_pager($GLOBALS['DATABASE']->getsize('users'));
		$riterator = new x7database_iterator('users','name',$pager->min,$pager->max);
		while($uid = $riterator->get_next()){
			$user = new lib_user($uid);
			$users[] = array('username'=>$user->data_cache['name'],'group'=>$groups[$user->data_cache['group']],'uid'=>$uid);
		}
		
	}else{
		
		$pager = new lib_pager();
		$GLOBALS['OUTPUT']->search_results = true;
		
		if(strlen($query) > 2){
			$dbq = $GLOBALS['DATABASE']->get('users',array('name',$query),true);
			$data = $dbq ? $dbq->get_next() : array();
			if(!is_array($data))
				$data = array();
			
			foreach($data as $val){
				if(!$pager->item())
					continue;
					
				$user = new lib_user($val[1]);
				$users[] = array('uid'=>$user->data_cache['uid'],'username'=>$user->data_cache['name'],'group'=>$groups[$user->data_cache['group']]);
			}
			
			if(empty($users))
				$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->search_failed;
		}else
			$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->query_too_short;
	}
	
	$GLOBALS['OUTPUT']->query = $query;
	$GLOBALS['OUTPUT']->users = $users;
	$GLOBALS['OUTPUT']->pages = $pager->get_pages();
	$GLOBALS['OUTPUT']->page = $pager->get_page();
	$GLOBALS['OUTPUT']->form = $form->get_fields();

	echo tpl_management::management();
?>